﻿Imports System.Data.OleDb

Public Class Details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Session("username") = User.Identity.Name

            Dim sql As String = "select  liker from usersheetliking  where liker=@f1 and  sheetid=@f2 "
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            cmd.Parameters.AddWithValue("@f2", Request.QueryString("id").ToString)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If dr.HasRows Then

                LinkButton1.Enabled = False


            End If
        Catch ex As Exception

        End Try



    End Sub

    Protected Sub btn_AddComm_Click(sender As Object, e As EventArgs) Handles btn_AddComm.Click

        Dim sql As String = "INSERT INTO Comments (Author,Title,CommentBody,Sheet_fkID,RegDate) VALUES (@f1,@f2,@f3,@f4,@f5)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_CommTitle.Text)
        cmd.Parameters.AddWithValue("@f3", tb_commBody.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@f5", DateTime.Now.Date())

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btn_AddTagToDb_Click(sender As Object, e As EventArgs) Handles btn_AddTagToDb.Click
        Dim sql As String = "INSERT INTO SheetsTags (Sheets_fkID,Tags_fkID) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cb_Tags.Items
            If cb.Selected = True Then
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
    End Sub

    Protected Sub btn_AddNewTag_Click(sender As Object, e As EventArgs) Handles btn_AddNewTag.Click
        Dim sql As String = "INSERT INTO Tags (Tags) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_AddNewTag.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID DESC", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)

        Dim associate As String = "INSERT INTO SheetsTags (Sheets_fkID,Tags_fkID) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()
    End Sub

    Protected Sub likedatasource_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles likedatasource.Selecting

    End Sub

    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
        Try
            Session("username") = User.Identity.Name

            Dim sql As String = "select  liker from usersheetliking  where liker=@f1 and  sheetid=@f2 "
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            cmd.Parameters.AddWithValue("@f2", Request.QueryString("id").ToString)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If dr.HasRows Then

                Exit Sub

            End If


            likedatasource.Insert()
            likedatasource.Update()
            LinkButton1.Enabled = False

        Catch ex As Exception

        End Try


    End Sub

    Protected Sub btn_SaveRating_Click(sender As Object, e As EventArgs) Handles btn_SaveRating.Click

        Dim oleDbConn = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)


        Dim delPrevRatings = "DELETE FROM UserSheetRating WHERE Rater=@f1 AND SheetID=@f2"
        Dim oleDbCmd4 = New OleDbCommand(delPrevRatings, oleDbConn)
        oleDbCmd4.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbCmd4.Parameters.AddWithValue("@f2", Request.Params("ID"))
        oleDbConn.Open()
        oleDbCmd4.ExecuteNonQuery()
        

        Dim sql = "INSERT INTO UserSheetRating (Rating,Rater,SheetID) VALUES (@f1,@f2,@f3)"
        Dim oleDbCmd3 = New OleDbCommand(sql, oleDbConn)
        oleDbCmd3.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        oleDbCmd3.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbCmd3.Parameters.AddWithValue("@f3", Request.Params("ID"))
        oleDbCmd3.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbCmd3.Dispose()
        oleDbConn.Dispose()



    End Sub
End Class